type Constructor = new (...args: any[]) => {}

interface Person {
  getCreatedTime(): void
}

function LogTime<T extends Constructor>(target: T) {
  return class extends target {
    createdTime: Date
    constructor(...args: any[]) {
      super(...args)
      this.createdTime = new Date()
      console.log(this.createdTime)
    }
    getCreatedTime() {
      return `该对象的创建时间是: ${this.createdTime}`
    }
  }
}

@LogTime
class Person {
  name: string
  age: number
  constructor(name: string, age: number) {
    this.name = name
    this.age = age
  }

  speak() {
    console.log('你好呀！')
  }
}

const p1 = new Person('张三', 18)
console.log(p1) // {name: '张三', age: 18, createdTime: Tue Sep 10 2024 08:43:21 GMT+0800 (中国标准时间)}
console.log('p1', p1.getCreatedTime())

setTimeout(() => {
  const p2 = new Person('张三', 18);
  console.log('p2', p2.getCreatedTime())

  console.log('p1', p1.getCreatedTime())
}, 3000) 
